/*
 * $Id: endian.c 50 2009-02-10 05:48:05Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#include <xp/bas/endian.h>

xp_bool_t xp_is_endian_big (void)
{
#if defined(XP_ENDIAN_BIG)
	return xp_true;
#elif defined(XP_ENDIAN_LITTLE)
	return xp_false;
#else
	short one = 1;
	char* c = (char*)&one;
	return *c == 0;
#endif
}

xp_bool_t xp_is_endian_little (void)
{
#if defined(XP_ENDIAN_BIG)
	return xp_false;
#elif defined(XP_ENDIAN_LITTLE)
	return xp_true;
#else
	short one = 1;
	char* c = (char*)&one;
	return *c != 0;
#endif
}

xp_uint16_t xp_ntoh16 (xp_uint16_t x)
{
#if defined(XP_ENDIAN_BIG)
	return x;
#elif defined(XP_ENDIAN_LITTLE)
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint16_t)c[0] << 8) | 
	       ((xp_uint16_t)c[1] << 0);
#else
	if (xp_is_endian_big()) return x;
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint16_t)c[0] << 8) | 
	       ((xp_uint16_t)c[1] << 0);
#endif
}

xp_uint16_t xp_hton16 (xp_uint16_t x)
{
#if defined(XP_ENDIAN_BIG)
	return x;
#elif defined(XP_ENDIAN_LITTLE)
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint16_t)c[0] << 8) | 
	       ((xp_uint16_t)c[1] << 0);
#else
	if (xp_is_endian_big()) return x;
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint16_t)c[0] << 8) | 
	       ((xp_uint16_t)c[1] << 0);
#endif
}

xp_uint32_t xp_ntoh32 (xp_uint32_t x)
{
#if defined(XP_ENDIAN_BIG)
	return x;
#elif defined(XP_ENDIAN_LITTLE)
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint32_t)c[0] << 24) | 
	       ((xp_uint32_t)c[1] << 16) |
	       ((xp_uint32_t)c[2] << 8) | 
	       ((xp_uint32_t)c[3] << 0);
#else
	if (xp_is_endian_big()) return x;
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint32_t)c[0] << 24) | 
	       ((xp_uint32_t)c[1] << 16) |
	       ((xp_uint32_t)c[2] << 8) | 
	       ((xp_uint32_t)c[3] << 0);
#endif
}

xp_uint32_t xp_hton32 (xp_uint32_t x)
{
#if defined(XP_ENDIAN_BIG)
	return x;
#elif defined(XP_ENDIAN_LITTLE)
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint32_t)c[0] << 24) | 
	       ((xp_uint32_t)c[1] << 16) |
	       ((xp_uint32_t)c[2] << 8) | 
	       ((xp_uint32_t)c[3] << 0);
#else
	if (xp_is_endian_big()) return x;
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint32_t)c[0] << 24) | 
	       ((xp_uint32_t)c[1] << 16) |
	       ((xp_uint32_t)c[2] << 8) | 
	       ((xp_uint32_t)c[3] << 0);
#endif
}


#if defined(XP_HAVE_UINT64_T)
xp_uint64_t xp_nton64 (xp_uint64_t x)
{
#if defined(XP_ENDIAN_BIG)
	return x;
#elif defined(XP_ENDIAN_LITTLE)
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint64_t)c[0] << 56) | 
	       ((xp_uint64_t)c[1] << 48) |
	       ((xp_uint64_t)c[2] << 40) | 
	       ((xp_uint64_t)c[3] << 32) |
	       ((xp_uint64_t)c[5] << 24) | 
	       ((xp_uint64_t)c[6] << 16) |
	       ((xp_uint64_t)c[7] << 8) | 
	       ((xp_uint64_t)c[8] << 0);
#else
	if (xp_is_endian_big()) return x;
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint64_t)c[0] << 56) | 
	       ((xp_uint64_t)c[1] << 48) |
	       ((xp_uint64_t)c[2] << 40) | 
	       ((xp_uint64_t)c[3] << 32) |
	       ((xp_uint64_t)c[5] << 24) | 
	       ((xp_uint64_t)c[6] << 16) |
	       ((xp_uint64_t)c[7] << 8) | 
	       ((xp_uint64_t)c[8] << 0);
#endif
}

xp_uint64_t xp_hton64 (xp_uint64_t x)
{
#if defined(XP_ENDIAN_BIG)
	return x;
#elif defined(XP_ENDIAN_LITTLE)
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint64_t)c[0] << 56) | 
	       ((xp_uint64_t)c[1] << 48) |
	       ((xp_uint64_t)c[2] << 40) | 
	       ((xp_uint64_t)c[3] << 32) |
	       ((xp_uint64_t)c[5] << 24) | 
	       ((xp_uint64_t)c[6] << 16) |
	       ((xp_uint64_t)c[7] << 8) | 
	       ((xp_uint64_t)c[8] << 0);
#else
	if (xp_is_endian_big()) return x;
	xp_uint8_t* c = (xp_uint8_t*)&x;
	return ((xp_uint64_t)c[0] << 56) | 
	       ((xp_uint64_t)c[1] << 48) |
	       ((xp_uint64_t)c[2] << 40) | 
	       ((xp_uint64_t)c[3] << 32) |
	       ((xp_uint64_t)c[5] << 24) | 
	       ((xp_uint64_t)c[6] << 16) |
	       ((xp_uint64_t)c[7] << 8) | 
	       ((xp_uint64_t)c[8] << 0);
#endif
}
#endif
